package com.zgz.underwrite.action;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;

import com.zgz.underwrite.dao.DepartmentDao;
import com.zgz.underwrite.model.Department;
import com.zgz.underwrite.util.Page;

public class DepartmentAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3608573178875384179L;

	private Department dept;
	private DepartmentDao deptDao;
	
	private String[] chkSon = new String[]{};
	
	private Page page;

	public String newDept() {
		try {
			this.deptDao.save(this.dept);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return INPUT;
	}

	public String deleteDept() {
		try {
			this.deptDao.delete(this.dept);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ERROR;
	}
	
	public String deleteMultipleDept(){
		Department dept = null;
		List<Department> list = new ArrayList<Department>();
		for(String deptNo : chkSon){
			dept = new Department();
			dept.setSDeptNo(deptNo);
			list.add(dept);
		}
		try {
			this.deptDao.deleteAll(list);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ERROR;
	}

	public String findAllDept() {
		long totalCount = this.deptDao.getDeptCount();
		DetachedCriteria criteria = DetachedCriteria.forClass(Department.class);
		criteria.setProjection(null);
		criteria.setFetchMode("employees", FetchMode.DEFAULT);
		int start = 0;;
		if (page != null) {
			if ("first".equals(page.getPageOpera())) {
				start = 0;
			}
			if ("back".equals(page.getPageOpera())) {
				if (page.hasPreviousPage()) {
					start = (int) page.getStart()-Page.DEFAULT_PAGE_SIZE;
					page.setCurrentPageNo(page.getCurrentPageNo() - 1);
				}
			}
			if ("next".equals(page.getPageOpera())) {
				if (page.hasNextPage()) {
					page.setCurrentPageNo(page.getCurrentPageNo() + 1);
					start = (int)page.getStart();
				}else{
					start = (int) page.getStart();
				}
			}
			if ("last".equals(page.getPageOpera())) {
				start = (int) ((page.getTotalPageCount() - 1) * Page.DEFAULT_PAGE_SIZE);
			}
			
			if("turnPage".equals(page.getPageOpera())){
				if(page.getCurrentPageNo() <= page.getTotalPageCount() && page.getCurrentPageNo() >0){
					start = (int) ((page.getCurrentPageNo()-1) * Page.DEFAULT_PAGE_SIZE);
				}
				else{
					start = 0;
					page.setCurrentPageNo(1);
				}
			}

		} else {
			page = new Page();
		}
		List<Department> list = this.deptDao.findByPage(criteria, start,
				Page.DEFAULT_PAGE_SIZE);
		page.setTotalCount(totalCount);
		page.setList(list);
		page.setPageSize(list.size());
		super.request.setAttribute("page", page);
		return SUCCESS;
	}

	public String requsetEditDept() {
		Department dept = null;
		try {
			dept = this.deptDao.get(this.dept.getSDeptNo());
			super.request.setAttribute("dept", dept);
			return "editor";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ERROR;
	}

	public String editDept() {
		try {
			this.deptDao.update(this.dept);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ERROR;
	}

	public void setDept(Department dept) {
		this.dept = dept;
	}

	public Department getDept() {
		return dept;
	}

	public void setDeptDao(DepartmentDao deptDao) {
		this.deptDao = deptDao;
	}

	public DepartmentDao getDeptDao() {
		return deptDao;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public Page getPage() {
		return page;
	}

	public void setChkSon(String[] chkSon) {
		this.chkSon = chkSon;
	}

	public String[] getChkSon() {
		return chkSon;
	}

}
